package utils
{
	import flash.display.Stage;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.ui.Keyboard;

	public class Game_input
	{
		
		// the current state of the mouse
		public var mouse_is_down:Boolean = false;
		public var mouse_click_x:int = 0;
		public var mouse_click_y:int = 0;
		public var mouse_x:int = 0;
		public var mouse_y:int = 0;
		// the current state of the keyboard controls
		public var pressing:Object =
			{ up:0, down:0, left:0, right:0, fire:0 };
		// if mouselook is on, this is added to the chase camera
		public var camera_angle_x:Number = 0;
		public var camera_angle_y:Number = 0;
		public var camera_angle_z:Number = 0;
		// if this is true, dragging the mouse changes the camera angle
		public var mouse_look_mode:Boolean = true;
		// the game's main stage
		public var stage:Stage;
		
		public function Game_input(the_stage:Stage)
		{
			stage = the_stage;
			// get keypresses and detect the game losing focus
			stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
			stage.addEventListener(KeyboardEvent.KEY_UP, keyReleased);
			stage.addEventListener(Event.DEACTIVATE, lostFocus);
			stage.addEventListener(Event.ACTIVATE, gainFocus);
			stage.addEventListener(MouseEvent.MOUSE_DOWN, mouse_down);
			stage.addEventListener(MouseEvent.MOUSE_UP, mouse_up);
			stage.addEventListener(MouseEvent.MOUSE_MOVE, mouse_move);
		}
		
		private function mouse_move(e:MouseEvent):void
		{
			mouse_x = e.stageX;
			mouse_y = e.stageY;
			if (mouse_is_down && mouse_look_mode)
			{
				camera_angle_y = 90 * ((mouse_x - mouse_click_x) / stage.stageWidth);
				camera_angle_x = 90 * ((mouse_y - mouse_click_y) / stage.stageHeight);
			}
		}
		
		private function mouse_down(e:MouseEvent):void
		{
			trace('mouse_down at '+e.stageX+','+e.stageY);
			mouse_click_x = e.stageX;
			mouse_click_y = e.stageY;
			mouse_is_down = true;
		}
		
		private function mouse_up(e:MouseEvent):void
		{
			trace('mouse_up at '+e.stageX+','+e.stageY+' drag distance:'+ (e.stageX-mouse_click_x)+','+(e.stageY-mouse_click_y));
			mouse_is_down = false;
			if (mouse_look_mode)
			{
				// reset camera angle
				camera_angle_x = camera_angle_y = camera_angle_z = 0;
			}
		}
		
		private function keyPressed(event:KeyboardEvent):void
		{
			// qwer 81 87 69 82
			// asdf 65 83 68 70
			// left right 37 39
			// up down 38 40
			//trace("keyPressed " + event.keyCode);
			switch(event.keyCode)
			{
				case Keyboard.UP:
				case 87:
					pressing.up = true;
					break;
				case Keyboard.DOWN:
				case 83:
					pressing.down = true;
					break;
				case Keyboard.LEFT:
				case 65:
					pressing.left = true;
					break;
				case Keyboard.RIGHT:
				case 68:
					pressing.right = true;
					break;
				case Keyboard.SPACE:
					pressing.fire = true;
					break;
			}
		}
		
		private function gainFocus(event:Event):void
		{
			trace("Game received keyboard focus.");
		}
		
		// if the game loses focus, don't keep keys held down
		private function lostFocus(event:Event):void
		{
			trace("Game lost keyboard focus.");
			pressing.up = false;
			pressing.down = false;
			pressing.left = false;
			pressing.right = false;
			pressing.fire = false;
		}
		
		private function keyReleased(event:KeyboardEvent):void
		{
			switch(event.keyCode)
			{
				case Keyboard.UP:
				case 87:
					pressing.up = false;
					break;
				case Keyboard.DOWN:
				case 83:
					pressing.down = false;
					break;
				case Keyboard.LEFT:
				case 65:
					pressing.left = false;
					break;
				case Keyboard.RIGHT:
				case 68:
					pressing.right = false;
					break;
				case Keyboard.SPACE:
					pressing.fire = false;
					break;
			}
		}
		
		
		
		/**end*/
	}
}